package cn.stylefeng.guns.modular.api.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtils;
import cn.stylefeng.guns.modular.api.entity.Purchase;
import cn.stylefeng.guns.modular.api.model.enums.PlanStatusEnum;
import cn.stylefeng.guns.modular.api.model.enums.PurchaseStatusEnum;
import cn.stylefeng.guns.modular.api.model.params.PurchaseParam;
import cn.stylefeng.guns.modular.api.service.PurchaseService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 采购控制器
 *
 * @author samwuu
 * @Date 2019-09-27 14:13:45
 */
@Controller
@RequestMapping("/api/purchase")
public class PurchaseController extends BaseController {

    private String PREFIX = "/purchase";

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 跳转到主页面
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/purchase.html";
    }

    /**
     * 新增页面
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/purchase_add.html";
    }

    /**
     * 编辑页面
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/purchase_edit.html";
    }

    /**
     * 新增接口
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PurchaseParam purchaseParam) {

        purchaseParam.setStatus(PurchaseStatusEnum.APPEAL.getCode());
        purchaseParam.setAppealTimeStart(DateUtil.formatDate(DateUtils.getNowDate()));
        this.purchaseService.add(purchaseParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @Transactional
    @PostMapping("/editItem")
    @ResponseBody
    @ApiOperation(value = "更新接口", notes = "更新", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "更新", response = ResponseData.class)})
    public ResponseData editItem(PurchaseParam purchaseParam) {
       String today = DateUtil.formatDate(DateUtils.getNowDate());

       if (purchaseParam.getStatus().equals(PurchaseStatusEnum.APPEAL.getCode())) {
            purchaseParam.setStatus(PurchaseStatusEnum.ORDERED.getCode());
            purchaseParam.setOrderTime(today);
        }else if (purchaseParam.getStatus().equals(PurchaseStatusEnum.ORDERED.getCode())) {
            purchaseParam.setStatus(PurchaseStatusEnum.INPUT.getCode());
            purchaseParam.setInputTime(today);
        }else if (purchaseParam.getStatus().equals(PurchaseStatusEnum.INPUT.getCode())) {
            purchaseParam.setStatus(PurchaseStatusEnum.DONE.getCode());
            purchaseParam.setDoneTime(today);

        }


        this.purchaseService.update(purchaseParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PurchaseParam purchaseParam) {
        this.purchaseService.delete(purchaseParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public ResponseData detail(PurchaseParam purchaseParam) {
        Purchase detail = this.purchaseService.getById(purchaseParam.getPurchaseId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author samwuu
     * @Date 2019-09-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PurchaseParam purchaseParam) {
        return this.purchaseService.findPageBySpec(purchaseParam);
    }

}


